//
//  EFAdView.m
//  EFHealth
//
//  Created by nf on 17/2/10.
//  Copyright © 2018年 ef. All rights reserved.
//

#import "EFAdView.h"

#import "UIImageView+WebCache.h"

@interface EFAdView()<UIScrollViewDelegate>
{
    UIImageView *_leftImageView ;
    UIImageView *_centerImageView ;
    UIImageView *_rightImageView ;
    
    NSUInteger _centerImageIndexl ;
    NSUInteger _leftImageIndex ;
    NSUInteger _rightImageIndex ;
    
    BOOL _isTimeUp ;
}

@property (nonatomic,strong)NSArray *imageURLArray ;
@property (nonatomic,strong)NSArray *titleArray ;
@property (nonatomic,strong)NSArray *modelArray ;

@property (nonatomic,assign)NSTimer *moveTimer ;

@property (nonatomic,assign)BOOL isNeedCycleRoll ;

@property (nonatomic,strong)UILabel *adTitleLabel;
@property (nonatomic,strong)UIPageControl *adPageControl ;


@end


@implementation EFAdView

+ (instancetype)adScrollViewWithFrame:(CGRect)frame
                           modelArray:(NSArray *)modelArray
                    imagePropertyName:(NSString *)imageName
                    titlePropertyName:(NSString *)titleName
{
    if (modelArray.count == 0) {
        return nil ;
    }
    EFAdView *view = [[EFAdView alloc]initWithFrame:frame];
    view.modelArray = modelArray ;
    NSMutableArray *tempTitleArr = [[NSMutableArray alloc]initWithCapacity:5];
    
    for (int i = 0; i < modelArray.count; i++) {
        NSString *title = @"";
        [tempTitleArr addObject:title];
    }
    view.titleArray = tempTitleArr ;
    view.imageURLArray = modelArray ;
    
    return view ;
    
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        _adScrollView = [[UIScrollView alloc]initWithFrame:self.bounds];
        _adScrollView.bounces = NO ;
        _adScrollView.delegate = self ;
        _adScrollView.pagingEnabled = YES ;
        _adScrollView.showsVerticalScrollIndicator = NO ;
        _adScrollView.showsHorizontalScrollIndicator = NO ;
        _adScrollView.backgroundColor = [UIColor whiteColor];
        _adScrollView.contentOffset = CGPointMake(self.width, 0);
        _adScrollView.contentSize = CGSizeMake(self.width*3, self.height);
        _adScrollView.contentInset = UIEdgeInsetsZero ;
        
        _leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height)];
        [_adScrollView addSubview:_leftImageView];
        
        _rightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.width*2, 0, self.width, self.height)];
        [_adScrollView addSubview:_rightImageView];
        
        _centerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.width, 0, self.width, self.height)];
        [_centerImageView addTapCallBack:self sel:@selector(iamgeTap)];
        [_adScrollView addSubview:_centerImageView];
        
        
        _isNeedCycleRoll = YES ;
        
        [self addSubview:_adScrollView];
    }
    return self;
}
- (void)iamgeTap
{
    if (_callBack && self.modelArray.count){
        _callBack(_centerImageIndexl,self.modelArray[_centerImageIndexl]);
    }
}
- (void)willMoveToSuperview:(UIView *)newSuperview
{
    if (nil == newSuperview) {
        [self.moveTimer invalidate];
        _moveTimer = nil ;
    }
    else{
        [self setUpTimer];
    }
}
- (void)setUpTimer
{
    if (_isNeedCycleRoll && self.imageURLArray.count>1) {
        _moveTimer = [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(animalMoveImage:) userInfo:nil repeats:YES];
        _isTimeUp = NO ;
    }
}
- (void)setIsNeedCycleRoll:(BOOL)isNeedCycleRoll
{
    _isNeedCycleRoll = isNeedCycleRoll ;
    if (!isNeedCycleRoll) {
        [self.moveTimer invalidate] ;
        _moveTimer = nil ;
    }
}
- (void)animalMoveImage:(NSTimer *)timer
{
    [_adScrollView setContentOffset:CGPointMake(self.width*2, 0) animated:YES];
    _isTimeUp = YES ;
    [NSTimer scheduledTimerWithTimeInterval:0.4f target:self selector:@selector(scrollViewDidEndDecelerating:) userInfo:nil repeats:NO];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (_adScrollView.contentOffset.x == 0) {
        _centerImageIndexl -= 1 ;
        _leftImageIndex -= 1 ;
        _rightImageIndex -= 1 ;
        if (_leftImageIndex == -1) {
            _leftImageIndex = _imageURLArray.count -1 ;
        }
        if (_centerImageIndexl == -1) {
            _centerImageIndexl = _imageURLArray.count - 1 ;
        }
        if (_rightImageIndex == -1) {
            _rightImageIndex = _imageURLArray.count - 1 ;
        }
    }
    else if(_adScrollView.contentOffset.x == self.width * 2)
    {
        _centerImageIndexl += 1 ;
        _leftImageIndex += 1 ;
        _rightImageIndex += 1 ;
        if (_leftImageIndex == _imageURLArray.count) {
            _leftImageIndex = 0 ;
        }
        if (_centerImageIndexl == _imageURLArray.count) {
            _centerImageIndexl = 0 ;
        }
        if (_rightImageIndex == _imageURLArray.count) {
            _rightImageIndex = 0 ;
        }
    }
    else{
        return;
    }
    [_adScrollView setContentOffset:CGPointMake(self.width, 0) animated:NO];
    
    
    [_leftImageView sd_setImageWithURL:[NSURL URLWithString:_imageURLArray[_leftImageIndex]] placeholderImage:kDefaultImage];
    [_centerImageView sd_setImageWithURL:[NSURL URLWithString:_imageURLArray[_centerImageIndexl]] placeholderImage:kDefaultImage];
    [_rightImageView sd_setImageWithURL:[NSURL URLWithString:_imageURLArray[_rightImageIndex]] placeholderImage:kDefaultImage];
    
    
    self.adPageControl.currentPage = _centerImageIndexl;
    if (_centerImageIndexl<=self.titleArray.count-1){
        self.adTitleLabel.text = self.titleArray[_centerImageIndexl];
    }
    
    
    //手动控制图片滚动应该取消那个三秒的计时器
    if (!_isTimeUp) {
        [self.moveTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:3.0f]];
    }
    _isTimeUp = NO;
    
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.moveTimer invalidate];
    self.moveTimer = nil;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self setUpTimer];
}

- (void)setImageURLArray:(NSArray *)imageURLArray
{
    _imageURLArray = imageURLArray ;
    
    _leftImageIndex = imageURLArray.count - 1 ;
    _centerImageIndexl = 0 ;
    _rightImageIndex = 1 ;
    if (imageURLArray.count == 1) {
        _adScrollView.scrollEnabled = NO ;
        _rightImageIndex = 0 ;
    }

    [_leftImageView sd_setImageWithURL:[NSURL URLWithString:_imageURLArray[_leftImageIndex]] placeholderImage:kDefaultImage];
    [_centerImageView sd_setImageWithURL:[NSURL URLWithString:_imageURLArray[_centerImageIndexl]] placeholderImage:kDefaultImage];
    [_rightImageView sd_setImageWithURL:[NSURL URLWithString:_imageURLArray[_rightImageIndex]] placeholderImage:kDefaultImage];
}


- (void)setTitleArray:(NSArray *)titleArray
{
    _titleArray = titleArray ;
    
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, self.height-30, self.width, 30)];
//    bgView.backgroundColor = [UIColor blackColor];
    bgView.alpha = 0.6;
    [self addSubview:bgView];
    
    [bgView addSubview:self.adPageControl];
    [bgView addSubview:self.adTitleLabel];
    
}

- (UIPageControl *)adPageControl
{
    if (nil == _adPageControl) {
        _adPageControl = [[UIPageControl alloc]init];
        _adPageControl.numberOfPages = self.titleArray.count ;
        _adPageControl.frame = CGRectMake( (self.width - 20*_adPageControl.numberOfPages)/2, 5 , 20*_adPageControl.numberOfPages, 20);
        _adPageControl.currentPageIndicatorTintColor = [UIColor redColor];
        _adPageControl.pageIndicatorTintColor = [UIColor grayColor];
        
        _adPageControl.currentPage = 0;
        _adPageControl.enabled = NO;
    }
    return _adPageControl ;
}
- (UILabel *)adTitleLabel
{
    if (nil == _adTitleLabel) {
        _adTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(3, 0, self.width-20, 30)];
        _adTitleLabel.backgroundColor = [UIColor clearColor];
        _adTitleLabel.textColor = [UIColor lightGrayColor];
        _adTitleLabel.font = kFontMidden ;
        _adTitleLabel.text = _titleArray[_centerImageIndexl];
    }
    return _adTitleLabel ;
}

@end

















